package com.store.app.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.adapter.OrderDetailsAdapter;
import com.store.app.bean.EventBusBean;
import com.store.app.bean.WypDetailsListBean;
import com.store.app.imlife.utils.ToastTools;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.SettingData;
import com.store.app.utils.Utils;
import com.store.app.widget.MyListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * 我要批订单详情页面
 */
public class OrderDetailsActivity extends BaseActivity implements View.OnClickListener, IOnHttpListener {


    private NetRequest netRequest;
    private String order_id;
    private ImageView public_ll_return;
    private TextView tvTitle;
    private TextView tv_order;
    private TextView tv_copy;
    private MyListView lv;
    private TextView tv_total;
    private TextView tv_youhui;
    private TextView tv_peisongfei;
    private TextView tv_shifu;
    private TextView tv_pay_way;
    private TextView tv_create_time;
    private TextView tv_contact;
    private TextView tv_address;
    private TextView tv_remark;
    private TextView tv_all_count;

    private String total_fee;
    private String item_num;
    private String payment_way_key;
    private String order_date;
    private String contact_tel;
    private String contact_person;
    private String deliver_address;
    private String remark;
    private String order_no;
    private String status;
    private String delivery_fee;
    private String sale_fee;
    private LinearLayout llRemark;
    private List<WypDetailsListBean> listBeanList;
    private OrderDetailsAdapter orderDetailsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.order_details);


        order_id = getIntent().getStringExtra("order_id");
        total_fee = getIntent().getStringExtra("total_fee");
        sale_fee = getIntent().getStringExtra("sale_fee");
        delivery_fee = getIntent().getStringExtra("delivery_fee");
        item_num = getIntent().getStringExtra("item_num");
        payment_way_key = getIntent().getStringExtra("payment_way_key");
        order_date = getIntent().getStringExtra("order_date");
        contact_tel = getIntent().getStringExtra("contact_tel");
        contact_person = getIntent().getStringExtra("contact_person");
        deliver_address = getIntent().getStringExtra("deliver_address");
        remark = getIntent().getStringExtra("remark");
        order_no = getIntent().getStringExtra("order_no");
        status = getIntent().getStringExtra("status");

        initView();
        initData();
        initEvent();
    }

    private void initView() {


        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        public_ll_return.setOnClickListener(this);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        llRemark = (LinearLayout) findViewById(R.id.ll_remark);
        tv_order = (TextView) findViewById(R.id.tv_order);
        tv_copy = (TextView) findViewById(R.id.tv_copy);
        tv_all_count = (TextView) findViewById(R.id.tv_all_count);
        tv_copy.setOnClickListener(this);
        lv = (MyListView) findViewById(R.id.lv);
        tv_total = (TextView) findViewById(R.id.tv_total);
        tv_youhui = (TextView) findViewById(R.id.tv_youhui);
        tv_youhui.setOnClickListener(this);
        tv_peisongfei = (TextView) findViewById(R.id.tv_peisongfei);
        tv_peisongfei.setOnClickListener(this);
        tv_shifu = (TextView) findViewById(R.id.tv_shifu);
        tv_pay_way = (TextView) findViewById(R.id.tv_pay_way);
        tv_pay_way.setOnClickListener(this);
        tv_create_time = (TextView) findViewById(R.id.tv_create_time);
        tv_create_time.setOnClickListener(this);
        tv_contact = (TextView) findViewById(R.id.tv_contact);
        tv_contact.setOnClickListener(this);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_address.setOnClickListener(this);
        tv_remark = (TextView) findViewById(R.id.tv_remark);
        tv_remark.setOnClickListener(this);
    }


    private String solvePayWay(String payType) {
        String payWayStr;
        if (payType.equals(SettingData.PAYMENT_WAY_01)) {
            payWayStr = "支付宝";
        } else if (payType.equals(SettingData.PAYMENT_WAY_02)) {
            payWayStr = "微信";
        } else if (payType.equals(SettingData.PAYMENT_WAY_03)) {
            payWayStr = "银联";
        } else if (payType.equals(SettingData.PAYMENT_WAY_04)) {
            payWayStr = "现金";
        } else if (payType.equals(SettingData.PAYMENT_WAY_05)) {
            payWayStr = "零钱";
        } else if (payType.equals(SettingData.PAYMENT_WAY_06)) {
            payWayStr = "礼券";
        } else if (payType.equals(SettingData.PAYMENT_WAY_05)) {
            payWayStr = "亲情卡";
        } else if (payType.equals(SettingData.PAYMENT_WAY_08)) {
            payWayStr = "金币";
        } else if (payType.equals(SettingData.PAYMENT_WAY_09)) {
            payWayStr = "积分";
        } else if (payType.equals(SettingData.PAYMENT_WAY_10)) {
            payWayStr = "支付宝扫描支付";
        } else if (payType.equals(SettingData.PAYMENT_WAY_11)) {
            payWayStr = "微信扫描支付";
        } else if (payType.equals(SettingData.PAYMENT_WAY_12)) {
            payWayStr = "翼支付";
        } else if (payType.equals(SettingData.PAYMENT_WAY_14)) {
            payWayStr = "翼支付扫码支付";
        } else {
            payWayStr = "其它";
        }
        return payWayStr;
    }


    private void initData() {
        netRequest = new NetRequest(this);
        netRequest.requestwypOrderItemForStoresFind(1, order_id);
        tv_shifu.setText(total_fee);
        tv_order.setText(order_no);
        tv_peisongfei.setText(delivery_fee);
        tvTitle.setText("详情");
        tv_total.setText(sale_fee);
        tv_all_count.setText(item_num);


        tv_pay_way.setText(solvePayWay(payment_way_key));
        tv_create_time.setText( order_date);
        tv_contact.setText(contact_person + " " + contact_tel);
        tv_address.setText(deliver_address);
        if(!TextUtils.isEmpty(remark)){
            llRemark.setVisibility(View.VISIBLE);
            tv_remark.setText(remark);
        }else{
            llRemark.setVisibility(View.GONE);
        }

        listBeanList = new ArrayList<>();
        orderDetailsAdapter = new OrderDetailsAdapter(this, listBeanList);
        lv.setAdapter(orderDetailsAdapter);
        Utils.Setlistview(lv);

    }

    private void initEvent() {
        EventBus.getDefault().register(this);
//        mReturn.setOnClickListener(this);
//        mOrderCopy.setOnClickListener(this);
//        mOrderCancel.setOnClickListener(this);
//        mOrderPayment.setOnClickListener(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEvent(EventBusBean eventBusBean) {
        if (eventBusBean.type == 207) {
            finish();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.public_ll_return:
                //返回
                finish();
                break;

            case R.id.tv_copy:
                //复制订单号
                String result = copy(order_no);
                if (!result.isEmpty()) {
                    Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show();
                }
                break;
            default:

                break;

        }
    }

    //复制文本
    public String copy(String content) {
        // 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(content);
        return cmb.getText().toString().trim();
    }


    @Override
    public void onAfinalFail() {
        ToastTools.showShort(this, "请检查本地网络");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                String doc_url = jsonObject.getString("doc_url");
                Map map = Utils.getMapForJson(doc_url);
                JSONArray jsonArray = jsonObject.getJSONArray("list");
                Gson gson = new Gson();
                listBeanList.clear();
                for (int i = 0; i < jsonArray.length(); i++) {
                    WypDetailsListBean wypDetailsListBean = new WypDetailsListBean();
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    wypDetailsListBean = gson.fromJson(jsonObject1.toString(), WypDetailsListBean.class);
                    wypDetailsListBean.setMap(map);
                    listBeanList.add(wypDetailsListBean);
                }
                orderDetailsAdapter.notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        ToastTools.showShort(this, msg);
    }
}
